package main

import (
	"fmt"
	"math/rand"
)

func GenerateIntA(done chan struct{}) chan int {
	ch := make(chan int, 2)
	go func() {
	Label:
		for {
			var d = rand.Int()
			select {
			case ch <- d:
				fmt.Printf("待打印: %d\n", d)
			case <- done:
				break Label
			}
		}
		close(ch)
		fmt.Println("没有数据了")
	}()

	return ch
}

func print(ch chan int, done chan struct{}) {
	fmt.Printf("当前打印：%d\n", <-ch)

	close(done)

	for p := range ch {
		fmt.Printf("当前打印：%d\n", p)
	}
	fmt.Println("打印完成！")
}

func main()  {
	done := make(chan struct{})
	ch := GenerateIntA(done)
	print(ch, done)
}
